home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / clgraph1.zip / CL_GRAPH.H next >
C/C++ Source or Header  |  1993-09-02  |  18KB  |  511 lines

  1. #ifndef _2D_H
  2. #define _2D_H
  3.  
  4. #if  defined(__DLL__)
  5. #  define _EXPORT _export
  6. #else
  7. #  define _EXPORT _CLASSTYPE
  8. #endif
  9.  
  10.  
  11. extern "C" {
  12. #include "windows.h"
  13. }
  14.  
  15.  
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <math.h>
  19.  
  20. #include "cl_list.cpp"
  21.  
  22. #define  MEM_ERROR           "Unable to Allocate Memory"
  23.  
  24. #define  NAME_SIZE           20
  25. #define  WIN_TITLE_SIZE      100
  26. #define  WIN_PTR1            "Clumsy_Ptr1"
  27. #define  WIN_PTR2            "Clumsy_Ptr2"
  28.  
  29. #define  BLACK               RGB( 0, 0, 0 )              // color black
  30. #define  WHITE               RGB( 0xFF, 0xFF, 0xFF )     // color white
  31. #define  RED                 RGB( 0xFF, 0, 0 )           // color red
  32. #define  GREEN               RGB( 0, 0xFF, 0 )           // color green
  33. #define  BLUE                RGB( 0, 0, 0xFF )           // color blue
  34.  
  35.  
  36. extern const  HBRUSH NO_BRUSH;                           // null brush
  37.  
  38. extern HANDLE hInst;
  39. extern HANDLE hPrevInst;
  40. extern int    cmdShow;
  41. extern LPSTR  cmdLine;
  42.  
  43. extern void * operator new(size_t size);
  44. extern void operator delete(void * obj);
  45.  
  46.  
  47. LRESULT CALLBACK _export WinProc( HWND hWnd, UINT iMessage,
  48.                                   WPARAM wParam, LPARAM lParam );
  49.  
  50.  
  51. typedef char boolean;
  52.  
  53.  
  54. // window object class
  55. class _EXPORT WinObj
  56. {
  57. public:
  58.   // display a message with a stop icon
  59.   virtual
  60.   void        Error( LPSTR str );
  61.  
  62.   // Display a message with an exclamation icon
  63.   virtual
  64.   void        Warning( LPSTR str );
  65.  
  66.   // Display a message box
  67.   virtual
  68.   int         Message( UINT type, LPSTR str );
  69. };
  70.  
  71.  
  72. // class with a cleanup flag
  73. // this class is inherited by any class that needs a flag to check for
  74. // a cleanup before an object fo that class is destroyed.
  75. class _EXPORT CleanUpCheck
  76. {
  77. protected:
  78.   boolean     cleanUpFlag;
  79.  
  80. public:
  81.               CleanUpCheck()                         { cleanUpFlag = FALSE; }
  82.   void        CleanUp( boolean cleanFlag=TRUE )  { cleanUpFlag = cleanFlag; }
  83. };
  84.  
  85.  
  86. // class for defining coordinates of a point in space
  87. // space is 2-dimentional in this version 
  88. class _EXPORT Coord
  89. {
  90. public:
  91.   float       x, y;
  92.  
  93.               Coord();           // default constructor initializes x = y = 0
  94.               Coord( float xCoor, float yCoor );
  95.   Coord       operator+( Coord& p );                  // return(this + p)
  96.   Coord       operator-();                            // return(-this)
  97.   Coord       operator-( Coord& p );                  // return(this - p)
  98.   Coord       operator*( Coord& p );                  // return(this * p)
  99.   Coord       Rot( float angle, Coord& p );           // return(this rotated
  100.                                                       // by angle around p)         
  101.   Coord&      operator+=( Coord& p );                 // this += p
  102.   Coord&      operator-=( Coord& p );                 // this -= p
  103.   Coord&      operator*=( Coord& p );                 // this *= p
  104.   Coord&      RotBy( float angle, Coord& p );         // rotate this by angle
  105.                                                       // around p
  106. };
  107.  
  108.  
  109. // Center is a global coordinate point representing (0,0)
  110. extern Coord Center;
  111.  
  112.  
  113. // abstract class for all graphic objects
  114. class _EXPORT VisualObject : public WinObj
  115. {
  116. public:
  117.   virtual     ~VisualObject()  {};                      // virtual destructor
  118.   // definition of virtual methods that apply to all graphic objects
  119.   virtual
  120.   void        Translate( Coord& transValue ) = 0;
  121.   virtual
  122.   void        Rotate( float angle, Coord& center = Center ) = 0;
  123.   virtual
  124.   void        Scale( Coord& scale, Coord& center = Center) = 0;
  125.   virtual
  126.   void        Draw( HDC dc, float angle = 0 ) = 0;
  127. };
  128.  
  129.  
  130. // define GraphicList as a linked list of visual object references
  131. typedef LinkedList<VisualObject&> GraphicList;
  132.  
  133.  
  134. // class for graphic objects consisting of a collection of graphic objects
  135. class _EXPORT ComplexGraphic : public VisualObject, public CleanUpCheck
  136. {
  137. private:
  138.   GraphicList container;
  139.  
  140. public:
  141.   virtual     ~ComplexGraphic();
  142.   void        AddObject( VisualObject& vObj );                 // adds object
  143.   void        Translate( Coord& transValue );
  144.   void        Rotate( float angle, Coord& center = Center );
  145.   void        Scale( Coord& scale, Coord& center = Center );
  146.   void        Draw( HDC dc, float angle = 0 );
  147. };
  148.  
  149.  
  150. // class for a space system for graphics 
  151. class _EXPORT Space : public ComplexGraphic
  152. {
  153. private:
  154.   long        infinity;                           // not used in this version
  155. };
  156.  
  157.  
  158. // class for storing Windows pens which are used to define the format
  159. // for drawing lines and arcs
  160. class _EXPORT PenObject
  161. {
  162. protected:                                   
  163.   LOGPEN      lp;                           // pen information
  164.  
  165. public:
  166.               PenObject( HPEN penValue );   // create the pen from an already
  167.                                             // existing pen
  168.               PenObject( COLORREF color );  // create a pen from a color
  169.   void        SetPen( HPEN penValue );      // changes the pen to a new one
  170.   void        SetPen( COLORREF color );     // changes the pen to a new color
  171.   COLORREF    GetColor();                   // returns the color of a pen
  172.   int         GetWidth();                   // returns the width of a pen
  173.   UINT        GetStyle();                   // returns the style of a pen
  174.   HPEN        ActivatePen( HDC dc );        // activates the pen on a device
  175.                                             // context
  176.   void        DeactivatePen( HDC dc, HPEN originalPen );
  177.                                             // removes the pen from a device
  178.                                             // context
  179. };
  180.  
  181.  
  182. // class for storing Windows brushes which are used to define the format
  183. // for filling areas
  184. class _EXPORT BrushObject
  185. {
  186. protected:
  187.   LOGBRUSH    lb;
  188. public:
  189.               BrushObject( HBRUSH brushValue );
  190.               BrushObject( COLORREF color );
  191.   void        SetBrush( HBRUSH brushValue );
  192.   void        SetBrush( COLORREF color );
  193.   COLORREF    GetColor();
  194.   int         GetHatch();
  195.   UINT        GetStyle();
  196.   HBRUSH      ActivateBrush( HDC dc );
  197.   void        DeactivateBrush( HDC dc, HPEN originalBrush );
  198. };
  199.  
  200.  
  201. // graphic class for points
  202. class _EXPORT Point : public PenObject, public VisualObject, protected Coord
  203. {
  204. public:
  205.               Point( float x, float y, HPEN pen ); 
  206.               Point( float x, float y, COLORREF color=BLACK );
  207.   void        Translate( Coord& transValue );
  208.   void        Rotate( float angle, Coord& center = Center );
  209.   void        Scale( Coord& scale, Coord& center = Center );
  210.   void        Draw( HDC dc, float angle = 0 );
  211.   Coord&      GetCoord();
  212. };
  213.  
  214.  
  215. // graphic class for lines
  216. class _EXPORT Line : public PenObject, public VisualObject
  217. {
  218. protected:
  219.   Coord       p1, p2;
  220.  
  221. public:
  222.  
  223.               Line( Coord& p1, Coord& p2, COLORREF color=BLACK );
  224.               Line( Coord& p1, Coord& p2, HPEN pen );
  225.   void        Translate( Coord& transValue );
  226.   void        Rotate( float angle, Coord& center = Center );
  227.   void        Scale( Coord& scale, Coord& center = Center );
  228.   void        Draw( HDC dc, float angle = 0 );
  229.   Coord&      GetCoord1();
  230.   Coord&      GetCoord2();
  231. };
  232.  
  233.  
  234. // graphic class for rectangles
  235. class _EXPORT CRectangle : public BrushObject, public Line
  236. {
  237. private:
  238.   void        Init( float angle );
  239.  
  240. protected:
  241.   Coord       p3, p4;
  242.  
  243. public:
  244.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  245.                           HPEN pen, HBRUSH brush=NO_BRUSH);
  246.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  247.                           HPEN pen, COLORREF brushColor );
  248.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  249.                           COLORREF penColor, HBRUSH brush=NO_BRUSH );
  250.               CRectangle( Coord& topLeft, Coord& bottomRight, float angle,
  251.                           COLORREF penColor, COLORREF brushColor );
  252.   void        Translate( Coord& transValue );
  253.   void        Rotate( float angle, Coord& center = Center );
  254.   void        Scale( Coord& scale, Coord& center = Center );
  255.   void        Draw( HDC dc, float angle = 0 );
  256.   Coord&      GetCoord3();
  257.   Coord&      GetCoord4();
  258. };
  259.  
  260.  
  261. // graphic class for ellipses
  262. class _EXPORT CEllipse : public CRectangle
  263. {
  264. public:
  265.               CEllipse( Coord& topLeft, Coord& bottomRight, HPEN pen,
  266.                         HBRUSH brush=NO_BRUSH );
  267.               CEllipse( Coord& topLeft, Coord& bottomRight, HPEN pen,
  268.                         COLORREF brushColor );
  269.               CEllipse( Coord& topLeft, Coord& bottomRight, COLORREF penCol,
  270.                         HBRUSH brush = NO_BRUSH);
  271.               CEllipse( Coord& topLeft, Coord& bottomRight, COLORREF pCol,
  272.                         COLORREF brushColor );
  273.   void        Draw( HDC dc, float angle = 0 );
  274. };
  275.  
  276.  
  277. // graphic class for circles
  278. class _EXPORT Circle : public CEllipse
  279. {
  280. private:
  281.   Coord       center;
  282.   float       radius;
  283.   void        RecalcEllipse( Coord& center, float radius );
  284.  
  285. public:
  286.               Circle( Coord& center, float radius, HPEN pen,
  287.                       HBRUSH brush=NO_BRUSH );
  288.               Circle( Coord& center, float radius, HPEN pen,
  289.                       COLORREF brushColor );
  290.               Circle( Coord& center, float radius, COLORREF penColor,
  291.                       HBRUSH brush=NO_BRUSH );
  292.               Circle( Coord& center, float radius, COLORREF penColor,
  293.                       COLORREF brushColor );
  294. };
  295.  
  296.  
  297. // graphic class for chords
  298. class _EXPORT CChord : public CEllipse
  299. {
  300. protected:
  301.   Coord       line1, line2;
  302.  
  303. public:
  304.               CChord( Coord& topLeft, Coord& bottomRight,
  305.                       Coord& line1, Coord& line2,
  306.                       HPEN pen, HBRUSH brush=NO_BRUSH );
  307.               CChord( Coord& topLeft, Coord& bottomRight,
  308.                       Coord& line1, Coord& line2,
  309.                       HPEN pen, COLORREF brushColor );
  310.               CChord( Coord& topLeft, Coord& bottomRight,
  311.                       Coord& line1, Coord& line2,
  312.                       COLORREF penColor, HBRUSH brush=NO_BRUSH );
  313.               CChord( Coord& topLeft, Coord& bottomRight,
  314.                       Coord& line1, Coord& line2,
  315.                       COLORREF penCol, COLORREF brushColor );
  316.   void        Translate( Coord& transValue );
  317.   void        Rotate( float angle, Coord& center = Center );
  318.   void        Scale( Coord& scale, Coord& center = Center );
  319.   void        Draw( HDC dc, float angle = 0 );
  320. };
  321.  
  322.  
  323. // graphic class for arcs
  324. class _EXPORT CArc : public CChord
  325. {
  326. public:
  327.               CArc( Coord& topLeft, Coord& bottomRight,
  328.                     Coord& line1, Coord& line2, HPEN pen );
  329.               CArc( Coord& topLeft, Coord& bottomRight,
  330.                     Coord& line1, Coord& line2, COLORREF penColor=BLACK );
  331.   void        Draw( HDC dc, float angle = 0 );
  332. };
  333.  
  334.  
  335. // graphic class for pies
  336. class _EXPORT CPie : public CChord
  337. {
  338. public:
  339.               CPie( Coord& topLeft, Coord& bottomRight,
  340.                     Coord& line1, Coord& line2,
  341.                     HPEN pen, HBRUSH brush=NO_BRUSH );
  342.               CPie( Coord& topLeft, Coord& bottomRight,
  343.                     Coord& line1, Coord& line2,
  344.                     HPEN pen, COLORREF brushColor );
  345.               CPie( Coord& topLeft, Coord& bottomRight,
  346.                     Coord& line1, Coord& line2,
  347.                     COLORREF penColor, HBRUSH brush=NO_BRUSH );
  348.               CPie( Coord& topLeft, Coord& bottomRight,
  349.                     Coord& line1, Coord& line2,
  350.                     COLORREF penColor, COLORREF brushColor );
  351.   void        Draw( HDC dc, float angle = 0 );
  352. };
  353.  
  354.  
  355. // graphic class for Windows window classes
  356. class _EXPORT WindowClass : public WinObj
  357. {
  358. private:
  359.   char        name[NAME_SIZE];     // name of the class
  360.   UINT        style;               // style of the class
  361.   HICON       icon;                // icon for minimized window of this class
  362.   HCURSOR     cursor;              // cursor for a window of this class
  363.   HBRUSH      background;          // background for a window of this class
  364.   LPCSTR      menuName;            // menu name for the menu of windows
  365.   HINSTANCE   instance;            // instance of the application
  366.  
  367. public:
  368.               WindowClass( char *className );
  369.               ~WindowClass();
  370.   boolean     Register();        // register the class
  371.   boolean     UnRegister();      // unregister the class.  Done by destructor
  372.   LPSTR       GetName();         // get the class name
  373.   // the following setting have to be done before the class is registered
  374.   // for them to take effect
  375.   void        SetIcon( HICON newIcon );             // sets the minimize icon
  376.   HICON       GetIcon();
  377.   void        SetCursor( HCURSOR newCursor );       // sets the window cursor
  378.   HCURSOR     GetCursor();
  379.   void        SetBackground( HBRUSH newBack );      // sets the bkgnd color
  380.   HBRUSH      GetBackground();
  381.   void        SetMenu( LPCSTR newMenu );            // set the window menu
  382.   LPCSTR      GetMenu();
  383.   void        SetInstance( HINSTANCE newInstance ); // sets application
  384.   HINSTANCE   GetInstance();  
  385. };
  386.  
  387.  
  388. // keyboard structure
  389. typedef struct {
  390.   int         repeatCount:16,
  391.               scanCode:8,
  392.               fExtended:1,
  393.               reserved1:4,
  394.               fAlt:1,
  395.               fPrev:1,
  396.               fTransition:1;
  397. } KeyCodes;
  398.  
  399.  
  400. // class for defining windows
  401. class _EXPORT Window: public WinObj, public CleanUpCheck
  402. {
  403. private:
  404.   HWND        handle;                        // Windows handle for the window
  405.   char        title[WIN_TITLE_SIZE];         // title of the window
  406.   LONG        style;                         // window style
  407.   int         x, y;                          // position of the window
  408.   int         width, height;                
  409.   HWND        parent;                        // parent of the window
  410.   HMENU       menu;                      
  411.  
  412. protected:
  413.   static
  414.   HANDLE      hAccel;                        // accelerator keys
  415.  
  416. public:
  417.               Window();
  418.               Window( HWND handle );         // create window object for
  419.                                              // existing window
  420.               ~Window();
  421.   HWND        GetHandle();
  422.   void        Create( WindowClass& wc );     // create the window
  423.  
  424.   virtual
  425.   LONG        MessageProc( UINT msg, WPARAM wParam, LPARAM lParam );
  426.   void        Move( RECT *rc, BOOL repaint=TRUE);
  427.   BOOL        Show( int nCmdShow = SW_SHOW );
  428.   LPSTR       GetTitle();
  429.   int         SetTitle( LPSTR pBuffer );
  430.   void        Close();
  431.   void        Delete();
  432.  
  433. #ifdef __BORLANDC__
  434.   #pragma warn -par
  435. #endif
  436.   // Virtual event methods that handle some of Windows events
  437.   virtual BOOL  InitMenu( HMENU menu )                   { return( FALSE ); };
  438.   virtual BOOL  QueryClose()                             { return( TRUE ); };
  439.   virtual BOOL  Size( UINT width, UINT height, UINT type );
  440.   virtual BOOL  Char( UINT value, UINT repeat )          { return( FALSE ); };
  441.   virtual BOOL  KeyDown(UINT virtKey, KeyCodes keyCodes) { return( FALSE ); };
  442.   virtual BOOL  LButtonDown( POINT cursor, UINT keys )   { return( FALSE ); };
  443.   virtual BOOL  LButtonDblClk( POINT cursor, UINT keys ) { return( FALSE ); };
  444.   virtual BOOL  MouseMove( POINT cursor, UINT keys )     { return( FALSE ); };
  445.   virtual BOOL  MouseActivate( HWND hTop, UINT hitTest ) { return( FALSE ); };
  446.   virtual BOOL  LButtonUp( POINT cursor, UINT keys )     { return( FALSE ); };
  447.   virtual BOOL  RButtonDown( POINT cursor, UINT keys )   { return( FALSE ); };
  448.   virtual BOOL  RButtonUp( POINT cursor, UINT keys )     { return( FALSE ); };
  449.   virtual BOOL  Paint();
  450.   virtual BOOL  SetFocus ( HWND hPrev )                  { return( FALSE ); };
  451.   virtual BOOL  KillFocus ( HWND hNext )                 { return( FALSE ); };
  452.   virtual BOOL  Command(UINT Id,UINT Code,HWND hControl);
  453.  
  454. #ifdef __BORLANDC__
  455.   #pragma warn +par
  456. #endif
  457.  
  458. };
  459.  
  460.  
  461. extern boolean SetWindowPtr( HWND hWnd, void *ptr );
  462. extern Window& GetWindowPtr( HWND hWnd );     // given window handle, returns
  463.                                               // a pointer to the window
  464.                                               // object
  465.  
  466.  
  467. // class for defining graphics windows
  468. class _EXPORT GraphicWindow : public Window
  469. {
  470. protected:
  471.   Space&      space;                   // the space containing the graphics
  472.                                        // to be displayed in this window
  473.   float       viewAngle;               // angle of rotation for the view
  474.   Coord       viewPoint;               // the point in space corresponding
  475.                                        // to the center of the window
  476.   int         bkMode;                  // Windows background mode
  477.   boolean     centerAlways;            // if true, the space will be centered
  478.  
  479. public:
  480.               GraphicWindow( Space& s );
  481.               GraphicWindow( Space& s, HWND handle );
  482.   void        Create( WindowClass& wc );
  483.   void        SetViewPoint( Coord& vp );
  484.   void        CenterView();
  485.   void        CenterAlways(boolean centerFlag=TRUE) {centerAlways=centerFlag;}
  486.   void        SetViewAngle( float angle );
  487.   float       GetViewAngle();
  488.   BOOL        Paint();
  489.   int         GetBkMode();
  490.   void        SetBkMode( int background );
  491. };
  492.  
  493.  
  494. // application class
  495. class _EXPORT ClumsyApp : public WinObj
  496. {
  497. protected:
  498.   virtual
  499.   void        MessageLoop();
  500.   int         status;                            // status of the application
  501.  
  502. public:
  503.               ClumsyApp( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  504.                          LPSTR lpszCmdLine, int nCmdShow );
  505.   virtual
  506.   int         Run();                             // run the application
  507.   int         GetStatus();                       // return the status
  508. };
  509.  
  510.  
  511. #endif